package com.efounder.form.comp;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import com.efounder.form.base.IComponent;
import com.efounder.form.group.IGroup;
import com.efounder.form.util.FormCompUtil;
import com.efounder.frame.ViewSize;
import com.efounder.util.AppContext;
import com.efounder.util.StringUtil;

/**
 * Created by Javor on 16/9/8.
 */

public class Group extends LinearLayout implements IComponent, IGroup, IUIComponent {
    private String id;

    private int height;
    private int width;

    private ViewSize size;

    private int paddingLeft;
    private int paddingRight;
    private int paddingTop;
    private int paddingBottom;

    // 内部控件对齐方式
    private String horizontalAlign;
    private String verticalAlign;

    private float x;
    private float y;

    private int percentWidth;
    private int percentHeight;

    private Paint mPaint;

    private IUIComponent parentComp;



    public Group() {
        super(AppContext.getInstance());
    }

    public Group(Context context) {
        super(context);
    }

    public Group(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void addChildComponent(IUIComponent component) {
        this.addView((View) component);
    }

    @Override
    public void creationComplete() {

    }

    @Override
    public void setLayout() {
        FormCompUtil.setLayoutSize(this);
        this.setGravity(FormCompUtil.getLayoutGravity(horizontalAlign,verticalAlign));

        this.setLayoutPadding();
    }

    public void setBorder(int borderColor, int borderWidth){
        if (borderWidth <= 0 ) return;
        mPaint = new Paint();
        mPaint.setColor(StringUtil.formColorToColor(borderColor));
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(borderWidth);
        this.invalidate();
    }
    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);

        if(mPaint == null)	return;
        canvas.drawLine(0, 0, this.getWidth(), 0, mPaint);
        canvas.drawLine(0, this.getHeight(), this.getWidth(), this.getHeight(), mPaint);
        canvas.drawLine(0, 0, 0, this.getHeight(), mPaint);
        canvas.drawLine(this.getWidth(), 0, this.getWidth(), this.getHeight(), mPaint);
    }
    /*
     * LinearLayout边框
     */
    public void setLayoutPadding() {
        this.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
    }

    @Override
    public float getX() {
        return x;
    }

    @Override
    public void setX(float x) {
        this.x = x;
        this.setPivotX(x);
    }

    @Override
    public float getY() {
        return y;
    }

    @Override
    public void setY(float y) {
        this.y = y;
        this.setPivotY(y);
    }

    @Override
    public int getGap() {
        return 0;
    }

    @Override
    public void setGap(int gap) {

    }

    @Override
    public String getID() {
        return id;
    }

    @Override
    public void setID(String id) {
        this.id = id;
    }

    @Override
    public void setCompWidth(int width) {
        this.width = width;
    }

    @Override
    public int getCompWidth(){
        return this.width;
    }

    @Override
    public void setCompHeight(int height) {
        this.height = height;
    }

    @Override
    public int getCompHeight() {
        return this.height;
    }

    @Override
    public int getPercentWidth() {
        return percentWidth;
    }

    @Override
    public void setPercentWidth(int percentW) {
        percentWidth = percentW;
    }

    @Override
    public int getPercentHeight() {
        return percentHeight;
    }

    @Override
    public void setPercentHeight(int percentH) {
        percentHeight = percentH;
    }

    @Override
    public ViewSize getViewSize() {
        return size;
    }

    @Override
    public void setViewSize(ViewSize size) {
        this.size = size;
    }

    public String getHorizontalAlign() {
        return horizontalAlign;
    }

    public void setHorizontalAlign(String horizontalAlign) {
        this.horizontalAlign = horizontalAlign;
    }

    public String getVerticalAlign() {
        return verticalAlign;
    }

    public void setVerticalAlign(String verticalAlign) {
        this.verticalAlign = verticalAlign;
    }

    @Override
    public int getPaddingLeft() {
        return paddingLeft;
    }

    public void setPaddingLeft(int paddingLeft) {
        this.paddingLeft = paddingLeft;
    }

    @Override
    public int getPaddingRight() {
        return paddingRight;
    }

    public void setPaddingRight(int paddingRight) {
        this.paddingRight = paddingRight;
    }

    @Override
    public int getPaddingTop() {
        return paddingTop;
    }

    public void setPaddingTop(int paddingTop) {
        this.paddingTop = paddingTop;
    }

    @Override
    public int getPaddingBottom() {
        return paddingBottom;
    }

    public void setPaddingBottom(int paddingBottom) {
        this.paddingBottom = paddingBottom;
    }
    @Override
    public IUIComponent getParentComp() {
        return parentComp;
    }

    @Override
    public void setParentComp(IUIComponent comp) {
        parentComp = comp;
    }
}
